package net.bwie.jtp.service.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.jtp.service.bean.DwsLogPageViewWindowReport;
import net.bwie.jtp.service.bean.TrafficPvReport;
import net.bwie.jtp.service.bean.TrafficUvReport;
import net.bwie.jtp.service.service.TrafficChannelStatsService;
import net.bwie.jtp.service.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: FuHe
 * @Date: 2025/5/21
 */
@RestController
@RequestMapping("/jtp/traffic")
public class TraffivController {
    private static final String DATE_DEFAULT_VALUE = "1970-07-01";
    @Autowired
    TrafficChannelStatsService trafficChannelStatsService;


    // 唯一访客数请求拦截方法
    @RequestMapping("/uvCount")
    public String getUvCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<TrafficUvReport> trafficUvReportList = trafficChannelStatsService.getUvCount(date);
        // 如果没有数据，直接返回空字符串
        if (trafficUvReportList == null || trafficUvReportList.isEmpty()){
            return "[]";
        }
        // 构建json字符串
        return JSON.toJSONString(trafficUvReportList);
    }

    /**
     *  渠道的页面访客数
     */
    @RequestMapping("/Channel/pvCount")
    public String getChannelPvCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<TrafficPvReport> trafficPvReportList = trafficChannelStatsService.getPvCount(date);
        // 如果没有数据，直接返回空字符串
        if (trafficPvReportList == null || trafficPvReportList.isEmpty()){
            return "[]";
        }
        // 构建json字符串
        return JSON.toJSONString(trafficPvReportList);
    }

    @RequestMapping("/Page/All")
    public String getPageAll(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<DwsLogPageViewWindowReport> trafficPvReportList = trafficChannelStatsService.getPageAll(date);
        // 如果没有数据，直接返回空字符串
        if (trafficPvReportList == null || trafficPvReportList.isEmpty()){
            return "[]";
        }
        // 构建json字符串
        return JSON.toJSONString(trafficPvReportList);
    }

}
